package test.json;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.FactoryBean;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.yuqih.common.json.JacksonObjectMapper;
import com.yuqih.common.json.ObjectIdJsonDeserializer;
import com.yuqih.common.json.ObjectIdJsonSerializer;

public class ObjectMapperFactoryBean implements FactoryBean<ObjectMapper> {

	private final ObjectMapper objectMapper;

	public ObjectMapperFactoryBean() {
		objectMapper = JacksonObjectMapper.newDefaultObjectMapperInstance();

		SimpleModule objectIdModule = new SimpleModule();
		objectIdModule.addSerializer(ObjectId.class, new ObjectIdJsonSerializer());
		objectIdModule.addDeserializer(ObjectId.class, new ObjectIdJsonDeserializer());
		objectMapper.registerModule(objectIdModule);
	}

	@Override
	public ObjectMapper getObject() throws Exception {
		return objectMapper;
	}

	@Override
	public Class<?> getObjectType() {
		return ObjectMapper.class;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}
